package com.wjx.shoolnews.Utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Map;

public class JwtUtils {
    private static final String SING="wjx!!@@##$$%%";
/**
 * 生成token header.playload.sing
 *
 */
    public static String getToken(Map<String,String> map) throws UnsupportedEncodingException {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,7);
//        创建jwt
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });

        String token=builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SING));
        return token;

    }

    public static DecodedJWT verify(String token) throws UnsupportedEncodingException {
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        return verify ;
    }





}
